import {Platform, SendMessageType} from '/@/config/DeclareGlobal';
import wsClient from '/@/core/communication/WsService';

class SendToUnity {
  private iframeDom: HTMLIFrameElement | undefined;

  constructor() {}

  public init(iframeDom: HTMLIFrameElement) {
    this.iframeDom = iframeDom;
  }

  // public Instance(iframeDom: HTMLIFrameElement): SendToUnity {
  //   if (!this.iframeDom) {
  //     this.init(iframeDom);
  //   }
  //   return this;
  // }

  public send(message: SendUnityData) {
    message.platform = Platform.web2unity;
    // this.iframeDom?.contentWindow?.postMessage(message, '*');
    // if(message.data.data !== "heartbeat")
    //   console.log('已通过SendToUnity转发任务消息到Unity', message);
    if (this.iframeDom && this.iframeDom.contentWindow) {
      this.iframeDom.contentWindow.postMessage(message, '*');
    }
    // else {
    // wsClient.sendPairingMessage(JSON.stringify(message));
    // }
    wsClient?.sendPairingMessage(JSON.stringify(message));
  }
}

const sendToUnity = new SendToUnity();
export default sendToUnity;
